package com.sport.facilities.booking.system.htmlchecker;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.InputTag;
import org.htmlparser.tags.OptionTag;
import org.htmlparser.tags.SelectTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import android.util.Log;

import com.sport.facilities.booking.system.core.HtmlHelper;
import com.sport.facilities.booking.system.model.MakeBookFormModel;

public class MakeBookingPageChecker extends BasicChecker{
	private final String TAG = "MakeBookingPageChecker";

	public MakeBookFormModel fnGetBookingModelFromHtml(String html) throws Exception {
		super.hasError(html);
		MakeBookFormModel bfm = new MakeBookFormModel();
		fnGetBookingModelFromHtml(html,bfm);
		Log.d(TAG, "BookingFormModel : "+bfm.toFormModelString());
		return bfm;
	}

	private void fnGetBookingModelFromHtml(String html, MakeBookFormModel bfm) throws ParserException {
		NodeList nodelist = HtmlHelper.getNodeListByTag(html, "table");
		fnGetNoteFromNodeList(nodelist.elementAt(0),bfm);
		fnGetInputNamesFromNodeList(nodelist,bfm);
		fnGetBookingDataFromTable(nodelist.elementAt(1),bfm);
	}

	private void fnGetBookingDataFromTable(Node n, MakeBookFormModel bfm) {
		if (n instanceof TableTag) {
			TableTag table = ((TableTag) n);
			if (table.getRowCount() > 0) {
				fnGetName(table.getRow(0),  bfm);
				fnGetBalanceDeposit(table.getRow(1),  bfm);
				fnGetActivity(table.getRow(3),  bfm);
				fnGetDate(table.getRow(4),  bfm);
				fnGetTime(table.getRow(5),  bfm);
				fnGetCentre(table.getRow(6),  bfm);
			}
		}
	}

	private void fnGetInputNamesFromNodeList(NodeList nodelist, MakeBookFormModel bfm) {
		TagNameFilter inputFilter = new TagNameFilter("input");
		NodeList foundNodeList = nodelist.extractAllNodesThatMatch(inputFilter,
				true);
		for (int i = 0; i < foundNodeList.size(); i++) {
			Node n = foundNodeList.elementAt(i);
			if (n instanceof InputTag) {
				InputTag iT = (InputTag) n;
				if (iT.getAttribute("type").equalsIgnoreCase("submit")) {
					bfm.setSubmitName(iT.getAttribute("name"));
					bfm.setSubmitValue(iT.getAttribute("value"));
				}
				if (iT.getAttribute("type").equalsIgnoreCase("text")) {
					bfm.setKaptchaName(iT.getAttribute("name"));
				}
			}
		}
	}

	private void fnGetBalanceDeposit(TableRow tr, MakeBookFormModel bfm) {
		if (tr == null) {
			return;
		}
		SEARCH_WHOLE_TR: for (int i = 0; i < tr.getColumnCount(); i++) {
			Node c = tr.getColumns()[i].getFirstChild();
			while (c != null) {
				if (c instanceof TextNode) {
					if (c.getText().contains("$")) {
						bfm.setCredit(c.getText());
						break SEARCH_WHOLE_TR;
					}
				}
				c = c.getNextSibling();
			}
		}
	}
	
	private void fnGetCentre(TableRow tr, MakeBookFormModel bfm) {
		if (tr == null) {
			return;
		}
		List<String> lst_centre = new ArrayList<String>();
		SEARCH_WHOLE_TR: for (int i = 0; i < tr.getColumnCount(); i++) {
			Node p = tr.getColumns()[i].getFirstChild();
			while (p != null) {
				if (p instanceof SelectTag) {
					SelectTag sNode = (SelectTag) p;
					bfm.setCentreSubmitName(sNode.getAttribute("name"));
					OptionTag[] tags = sNode.getOptionTags();
					for (int j = 0; j < tags.length; j++) {
						String a = tags[j].getOptionText();
						lst_centre.add(tags[j].getOptionText());
					}
					break SEARCH_WHOLE_TR;
				}
				p = p.getNextSibling();
			}
		}
		bfm.setLst_centre(lst_centre.toArray(new String[lst_centre.size()]));
	}
	
	private void fnGetActivity(TableRow tr, MakeBookFormModel bfm) {
		if (tr == null) {
			return;
		}
		List<String> lst_activity = new ArrayList<String>();
		SEARCH_WHOLE_TR: for (int i = 0; i < tr.getColumnCount(); i++) {
			Node p = tr.getColumns()[i].getFirstChild();
			while (p != null) {
				if (p instanceof Div) {
					Node c = p.getFirstChild();
					while (c != null) {
						if (c instanceof SelectTag) {
							SelectTag sNode = (SelectTag) c;
							bfm.setActivitySubmitName(sNode.getAttribute("name"));
							OptionTag[] tags = sNode.getOptionTags();
							for (int j = 0; j < tags.length; j++) {
								lst_activity.add(tags[j].getOptionText());
							}
							break SEARCH_WHOLE_TR;
						}
						c = c.getNextSibling();
					}
				}
				p = p.getNextSibling();
			}
		}
		bfm.setLst_activity(lst_activity.toArray(new String[lst_activity.size()]));
	}

	private void fnGetDate(TableRow tr, MakeBookFormModel bfm) {
		if (tr == null) {
			return;
		}
		List<String> lst_st_day = new ArrayList<String>();
		SEARCH_WHOLE_TR: for (int i = 0; i < tr.getColumnCount(); i++) {
			Node c = tr.getColumns()[i].getFirstChild();
			while (c != null) {
				if (c instanceof SelectTag) {
					SelectTag sNode = (SelectTag) c;
					bfm.setSt_daySubmitName(sNode.getAttribute("name"));
					OptionTag[] tags = sNode.getOptionTags();
					for (int j = 0; j < tags.length; j++) {
						lst_st_day.add(tags[j].getOptionText());
					}
					break SEARCH_WHOLE_TR;
				}
				c = c.getNextSibling();
			}
		}
		bfm.setLst_ed_day(lst_st_day.toArray(new String[lst_st_day.size()]));
		bfm.setLst_st_day(lst_st_day.toArray(new String[lst_st_day.size()]));
	}

	private void fnGetTime(TableRow tr, MakeBookFormModel bfm) {
		if (tr == null) {
			return;
		}
		List<String> lst_st_time = new ArrayList<String>();
		SEARCH_WHOLE_TR: for (int i = 0; i < tr.getColumnCount(); i++) {
			Node c = tr.getColumns()[i].getFirstChild();
			while (c != null) {
				if (c instanceof SelectTag) {
					SelectTag sNode = (SelectTag) c;
					bfm.setSt_timeSubmitName(sNode.getAttribute("name"));
					OptionTag[] tags = sNode.getOptionTags();
					for (int j = 0; j < tags.length; j++) {
						lst_st_time.add(tags[j].getOptionText());
					}
					break SEARCH_WHOLE_TR;
				}
				c = c.getNextSibling();
			}
		}
		bfm.setLst_ed_time(lst_st_time.toArray(new String[lst_st_time.size()]));
		bfm.setLst_st_time(lst_st_time.toArray(new String[lst_st_time.size()]));
	}

	private void fnGetName(TableRow tr, MakeBookFormModel bfm) {
		if (tr == null) {
			return;
		}
		if (tr.getColumnCount() > 0) {
			TableColumn[] cols = tr.getColumns();
			TableColumn col = cols[0];
			Node c = col.getFirstChild();
			while (c != null) {
				if (c instanceof TextNode) {
					bfm.setName(c.getText());
					break;
				}
				c = c.getNextSibling();
			}
		}
	}

	private void fnGetNoteFromNodeList(Node n, MakeBookFormModel bfm) {
		if (n instanceof TableTag) {
			TableTag table = ((TableTag) n);
			if (table.getRowCount() > 0) {
				TableRow row = table.getRow(0);
				if (row.getColumnCount() > 0) {
					TableColumn[] cols = row.getColumns();
					TableColumn col = cols[0];
					Node c = col.getFirstChild();
					while (c != null) {
						if (c instanceof TextNode) {
							bfm.setNote(c.getText());
							break;
						}
						c = c.getNextSibling();
					}
				}
			}
		}
	}
}
